package powtorka.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Malowanie extends JFrame {
	private class JasnyListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {

			kolor = losujJasnyKolor();
			panelRys.repaint();
			
		}

		private Color losujJasnyKolor() {

			int red = (int) (((Math.random() * 4) * 32) + 128);
			int green = (int) (((Math.random() * 4) * 32) + 128);
			int blue = (int) (((Math.random() * 4) * 32) + 128);
			return new Color(red, green, blue);
		}
	}

	private class CiemnyListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {

			kolor = losujCiemnyKolor();
			panelRys.repaint();
		}

		private Color losujCiemnyKolor() {
			int red = (int) (Math.random() * 128);
			int green = (int) (Math.random() * 128);
			int blue = (int) (Math.random() * 128);
			return new Color(red, green, blue);
		}

	}

	private class RysunkowyPanel extends JPanel {
		private static final long serialVersionUID = -4938431556344582400L;

		@Override
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.setColor(kolor);
			g.fillRect(0, 0, this.getWidth(), this.getHeight());
		}

	}

	private static final long serialVersionUID = 3864666882115421188L;
	private JButton przyciskCiemny;
	private JButton przyciskJasny;
	private RysunkowyPanel panelRys;
	private Color kolor;

	public Malowanie() {
		super("Losowe kolory");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new BorderLayout());
		panelRys = new RysunkowyPanel();
		panelRys.setPreferredSize(new Dimension(320, 240));
		add(panelRys, BorderLayout.CENTER);
		przyciskCiemny = new JButton("Kolory ciemne");
		przyciskJasny = new JButton("Kolory jasne");
		przyciskCiemny.addActionListener(new CiemnyListener());
		przyciskJasny.addActionListener(new JasnyListener());
		add(przyciskCiemny, BorderLayout.SOUTH);
		add(przyciskJasny, BorderLayout.NORTH);
		pack();
		setLocationRelativeTo(rootPane);
		setVisible(true);
	}
}
